<?php

require_once 'Pipes/Exception.php';
require_once 'Pipes/Model/Relationship/Mapper.php';
require_once 'PipesImpl/Model.php';
require_once 'PipesImpl/Visitor.php';

class Pipes_Model_Visitors_ExternalRelationshipRemover implements PipesImpl_Visitor
{
    private $_config = null;

    public function __construct(PipesImpl_Config $config) {
        $this->_config = $config;
    }

    public function visit(PipesImpl_Visitable $visitable) {
        if (!$visitable instanceof PipesImpl_Model) {
            throw new Pipes_Exception('Invalid visitable: does not implement PipesImpl_Model');
        }

        $model = $visitable;

        $relationshipMapper = new Pipes_Model_Relationship_Mapper($this->_config, $model);

        foreach ($model->listRelationships() as $rel) {
            foreach ($model->$rel as $relatedModel) {
                $relationshipMapper->remove($relatedModel);
            }
        }
    }
}
